package io.afu.solusvm.client;

import io.afu.httprequest.HttpRequest;
import io.afu.solusvm.dto.SolusVMResp;
import io.afu.solusvm.enums.ExceptionEnums;
import io.afu.solusvm.exception.SVException;

/**
 * @author RffanLAB.方露宇
 * @version 0.1
 */
public class SVClient {

    private String key;

    private String hash;

    private String apiUrl;

    public static SVClient build(){
        return new SVClient();
    }

    public SVClient setApiKeyUrl(String key,String hash,String apiUrl){
        this.key = key;
        this.hash = hash;
        this.apiUrl = apiUrl;
        return this;
    }

    public void check() throws SVException {
        if (this.key == null || this.key.equals("") || this.hash == null || this.hash.equals("") || this.apiUrl == null || this.apiUrl.equals("")){
            throw new SVException(ExceptionEnums.API_KEY_HASH_URL_NOT_SET);
        }
    }


    public SolusVMResp boot(){
        String result = HttpRequest.getInstance()
                .setUrl(apiUrl)
                .addParam("key",key)
                .addParam("hash",hash)
                .addParam("action","boot")
                .addParam("status","true")
                .get();
        System.out.println(result);
        return SolusVMResp.parse(result);
    }

    public SolusVMResp reboot(){
        String result = HttpRequest.getInstance()
                .setUrl(apiUrl)
                .addParam("key",key)
                .addParam("hash",hash)
                .addParam("action","reboot")
                .addParam("status","true")
                .get();
        return SolusVMResp.parse("");
    }

    public SolusVMResp shutdown(){
        String result = HttpRequest.getInstance()
                .setUrl(apiUrl)
                .addParam("key",key)
                .addParam("hash",hash)
                .addParam("action","shutdown")
                .addParam("status","true")
                .get();
        return SolusVMResp.parse(result);
    }

    public SolusVMResp status(){
        String result = HttpRequest.getInstance()
                .setUrl(apiUrl)
                .addParam("key",key)
                .addParam("hash",hash)
                .addParam("action","status")
                .addParam("status","true")
                .get();
        return SolusVMResp.parse(result);
    }

    public SolusVMResp info(){
        String result = HttpRequest.getInstance()
                .setUrl(apiUrl)
                .addParam("key",key)
                .addParam("hash",hash)
                .addParam("action","boot")
                .addParam("status","true")
                .addParam("ipaddr","true")
                .addParam("hdd","true")
                .addParam("mem","true")
                .addParam("bw","true")
                .get();
        return SolusVMResp.parse(result);
    }









}
